package org.jetlinks.community.mstn.term;

import org.hswebframework.ezorm.core.param.Term;
import org.hswebframework.ezorm.rdb.metadata.RDBColumnMetadata;
import org.hswebframework.ezorm.rdb.operator.builder.fragments.AbstractTermsFragmentBuilder;
import org.hswebframework.ezorm.rdb.operator.builder.fragments.PrepareSqlFragments;
import org.hswebframework.ezorm.rdb.operator.builder.fragments.SqlFragments;
import org.hswebframework.ezorm.rdb.operator.builder.fragments.term.AbstractTermFragmentBuilder;
import org.hswebframework.web.api.crud.entity.TermExpressionParser;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class MstnPropertiesTermBuilder extends AbstractTermFragmentBuilder {

    public static final String dimensionType = "mstn-json";

    public MstnPropertiesTermBuilder() {
        super(dimensionType, "根据properties查询设备");
    }

    @Override
    public SqlFragments createFragments(String columnFullName, RDBColumnMetadata rdbColumnMetadata, Term term) {
        PrepareSqlFragments fragments = PrepareSqlFragments.of();
        fragments.addSql("(" + columnFullName + "::json->>");
        Object value = term.getValue();
        if (value instanceof Map) {
            System.out.println("暂未实现");
        } else if (value instanceof List) {
            System.out.println("暂未实现");
        } else {
            String terms = String.valueOf(value);
            //json
            if (terms.startsWith("[")) {
                System.out.println("暂未实现");
            } else if (terms.startsWith("{")) {
                System.out.println("暂未实现");
            } else if (terms.contains(":") && !terms.contains(" ")) {
                System.out.println("暂未实现");
            } else {
                //SQL表达式
                List<Term> tagKeys = TermExpressionParser.parse(terms);
                fragments.addFragments(builder.createTermFragments(rdbColumnMetadata,tagKeys));
            }
        }
        return fragments;
    }

    static MstnPropertiesTermsBuilder builder = new MstnPropertiesTermsBuilder();

    static class MstnPropertiesTermsBuilder extends AbstractTermsFragmentBuilder<RDBColumnMetadata> {

        @Override
        protected SqlFragments createTermFragments(RDBColumnMetadata parameter, List<Term> terms) {
            return super.createTermFragments(parameter, terms);
        }

        @Override
        protected SqlFragments createTermFragments(RDBColumnMetadata tableOrViewMetadata, Term term) {
            PrepareSqlFragments sqlFragments = PrepareSqlFragments.of();

            switch (term.getTermType()) {
                case "eq" :
                    if (term.getValue() instanceof String) {
                        sqlFragments
                            .addSql("?").addParameter(term.getColumn())
                            .addSql("= ? )").addParameter(term.getValue());
                    } else {
                        sqlFragments
                            .addSql("?").addParameter(term.getColumn())
                            .addSql("= " + term.getValue() + " )");
                    }
                    break;
                case "gt" :
                    sqlFragments
                        .addSql("?").addParameter(term.getColumn())
                        .addSql(")::int > " + term.getValue());
                    break;
                case "lt":
                    sqlFragments
                        .addSql("?").addParameter(term.getColumn())
                        .addSql(")::int < " + term.getValue());
                    break;
                case "gte" :
                    sqlFragments
                        .addSql("?").addParameter(term.getColumn())
                        .addSql(")::int >= " + term.getValue());
                    break;
                case "lte":
                    sqlFragments
                        .addSql("?").addParameter(term.getColumn())
                        .addSql(")::int <= " + term.getValue());
                    break;
                default:
                    System.out.println("暂未实现");
                    break;

            }
//            sqlFragments.addSql("?").addParameter(term.getColumn())
//                .addSql(handleTermType(term.getTermType()) + " ")
//                .addSql(" ? ").addParameter(term.getValue())
//                .addSql(term.getTermType().equals("eq") ? " )" : "");
            return sqlFragments;
        }


        //处理term类型
        private String handleTermType(String termType) {
            switch (termType) {
                case "eq" :
                    return "=";
                case "gt" :
                    return ")::int >";
                case "lt":
                    return ")::int <";
                default:
                    return " " + termType;
            }
        }
    }

}
